package com.example.administrator.zhjt.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;

import com.example.administrator.zhjt.R;
import com.example.administrator.zhjt.bean.Road;

public class RoadAdapter extends BaseAdapter {

    private List<Road> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public RoadAdapter(List<Road> objects,Context context) {
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Road getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.road_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(Road object, ViewHolder holder) {
        //TODO implement
        holder.tvRoadId.setText(object.getRoadId()+"");
        holder.tvBalance.setText(object.getBalance()+"");
        if (object.getBalance()==0){
            holder.imgBalance.setImageResource(R.color.colorGreen);
        }else if (object.getBalance()==1){
            holder.imgBalance.setImageResource(R.color.colorBlue);
        }else if (object.getBalance()==2){
            holder.imgBalance.setImageResource(R.color.colorPrimary);
        }else if (object.getBalance()==3){
            holder.imgBalance.setImageResource(R.color.colorAccent);
        }else if (object.getBalance()==4){
            holder.imgBalance.setImageResource(R.color.colorRed);
        }
    }

    protected class ViewHolder {
        private TextView tvRoadId;
        private TextView tvBalance;
        private ImageView imgBalance;

        public ViewHolder(View view) {
            tvRoadId = (TextView) view.findViewById(R.id.tv_road_id);
            tvBalance = (TextView) view.findViewById(R.id.tv_balance);
            imgBalance = (ImageView) view.findViewById(R.id.img_balance);
        }
    }
}